package marke.barak.windows.composit;

import marek.barak.classes.Data;
import marek.barak.windows.main.main;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.SWT;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;

public class BrandChoose extends Composite {
	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
	TreeItem item;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public BrandChoose(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				main.c = new TypeOfRequest(getShell(), SWT.FILL);
				main.c.setSize(550, 400);
			}
		});
		
		final Tree tree = new Tree(this, SWT.BORDER);
		tree.setBounds(10, 10, 130, 312);
		formToolkit.adapt(tree);
		formToolkit.paintBordersFor(tree);
		
		TreeItem trtmZseries = new TreeItem(tree, SWT.NONE);
		trtmZseries.setText("zSeries");
		
		TreeItem trtmHw = new TreeItem(trtmZseries, SWT.NONE);
		trtmHw.setText("HW");
		trtmZseries.setExpanded(false);
		
		TreeItem trtmSsd = new TreeItem(tree, SWT.NONE);
		trtmSsd.setText("SSD");
		
		TreeItem trtmHw_1 = new TreeItem(trtmSsd, SWT.NONE);
		trtmHw_1.setText("HW");
		
		TreeItem trtmSw = new TreeItem(trtmSsd, SWT.NONE);
		trtmSw.setText("SW");
		trtmSsd.setExpanded(false);
		
		TreeItem trtmRss = new TreeItem(tree, SWT.NONE);
		trtmRss.setText("RSS");
		
		TreeItem trtmSw_1 = new TreeItem(trtmRss, SWT.NONE);
		trtmSw_1.setText("SW");
		
		TreeItem trtmHw_2 = new TreeItem(trtmRss, SWT.NONE);
		trtmHw_2.setText("HW");
		trtmRss.setExpanded(false);
		
		TreeItem trtmPower = new TreeItem(tree, SWT.NONE);
		trtmPower.setText("Power");
		
		TreeItem trtmSw_2 = new TreeItem(trtmPower, SWT.NONE);
		trtmSw_2.setText("SW");
		trtmPower.setExpanded(false);
		
		TreeItem trtmIseries = new TreeItem(tree, SWT.NONE);
		trtmIseries.setText("iSeries");
		
		TreeItem trtmSw_3 = new TreeItem(trtmIseries, SWT.NONE);
		trtmSw_3.setText("SW");
		trtmIseries.setExpanded(false);
		
		TreeItem trtmXseries = new TreeItem(tree, SWT.NONE);
		trtmXseries.setText("xSeries");
		
		TreeItem trtmSw_4 = new TreeItem(trtmXseries, SWT.NONE);
		trtmSw_4.setText("SW");
		trtmXseries.setExpanded(false);
		
		TreeItem trtmNewTreeitem = new TreeItem(tree, SWT.NONE);
		trtmNewTreeitem.setText("ISS");
		
		final TreeItem trtmNewTreeitem_1 = new TreeItem(trtmNewTreeitem, SWT.NONE);
		trtmNewTreeitem_1.setText("SW");
		trtmNewTreeitem_1.setExpanded(false);
		
		TreeItem trtmNewTreeitem_2 = new TreeItem(trtmNewTreeitem, SWT.NONE);
		trtmNewTreeitem_2.setText("HW");
		trtmNewTreeitem_2.setExpanded(false);
		
		trtmNewTreeitem.setExpanded(false);
		
		tree.addListener(SWT.MouseDown, new Listener(){
			public void handleEvent(Event event) {
				Point point = new Point(event.x,event.y);
				item = tree.getItem(point);
	    }});
		
		Button btnNewButton = new Button(this, SWT.CENTER);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(item!=null && item.getParentItem()!=null){
					Data.Builder.setBrand(item.getParentItem().getText() + " " + item.getText());
					main.c.dispose();
				}else{
					MessageBox msg = new MessageBox(getShell(),SWT.ICON_ERROR|SWT.ABORT);
					msg.setMessage("Error no item selected");
					msg.setText("Error");
				}
			}
		});
		btnNewButton.setBounds(382, 311, 68, 23);
		formToolkit.adapt(btnNewButton, true, true);
		btnNewButton.setText("Next");
		
	
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
